
/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.dc2owl.owl;

import java.io.*;




import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ModelFactory;

import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Property;

import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.ontology.*;

public class DublinCore2OntTranslator {

    /** Creates a new instance of DublinCore2OntTranslator */
    public DublinCore2OntTranslator() {

    }

    /**
     * translate the dublin core file to owl ontology file
     * @param dcstream dublin core example file
     * @param owlstream dublin core owl ontology file
     * @param auxstream output file
     */
    public void translate(InputStream dcstream, InputStream owlstream,
            OutputStream auxstream) {

        //namespace
        String ns =
                "http://www.cc.uah.es/ie/ont/dc-ospace#";

        //create the model
        OntModel model =
                ModelFactory.createOntologyModel();
        //open in read mode
        model.read(dcstream, "");

        //create the model
        OntModel model2 =
                ModelFactory.createOntologyModel();
        model2.read(owlstream, "");

        //get the class with a given name
        OntClass c =
                model2.getOntClass(ns + "Resource");

        //create the instance using the previous class
        Individual instancia =
                model2.createIndividual(ns + "Recurso1", c);

        //list the sentences of the model
        StmtIterator iter =
                model.listStatements();

        while (iter.hasNext()) {
            Statement stmt =
                    iter.nextStatement();
            Resource subject =
                    stmt.getSubject();     // get the subject
            Property predicate =
                    stmt.getPredicate();   // get the predicate
            RDFNode object =
                    stmt.getObject();      // get the object

            //get the name of the property
            String nomProp =
                    predicate.toString().substring(32);

            if (!(object instanceof Resource)) // object is a literal
            {
                DatatypeProperty propiedad =
                        model2.getDatatypeProperty(ns + nomProp);
                instancia.setPropertyValue(propiedad, object);
            }
        }
        //write in the model
        model2.write(auxstream, "RDF/XML");
    }
}
  